<script lang="ts" setup>
import axios, { type GetImgs, Status } from "@util/http"
import { showErrorAlert, showSuccessAlert } from "@util/showAlert"
import { useFileStore } from "@store/useFileStore"

let list = reactive<string[]>([])

axios.get<any, GetImgs>("/upload").then(res => {
  if (res.status === "success") {
    list.push(...res.data)
  } else {
    showErrorAlert("获取失败, 请刷新重试", "Get imgs error, please reload again")
  }
})

const { deleteUploadedFile } = useFileStore()

function deleteOne(index: number, name: string) {
  // 删除图片
  axios.delete<any, { status: Status }>("/upload", { params: { name } }).then(res => {
    if (res.status === "success") {
      // 删除已上传列表
      list.splice(index, 1)
      // 删除刚刚上传的列表
      deleteUploadedFile(name)
      showSuccessAlert("删除完成", "Delete finished")
    }
  }, err => {
    showErrorAlert("删除失败, 请刷新重试", "Delete error, please reload again")
  })
}
</script>

<template>
  <div class="my-list">
    <div class="tip">
      <div class="title">
        <img src="/tip.svg" alt="img load error">
        <p>{{$t("mine.tip")}}</p>
      </div>
      <div class="show">
        <p>{{$t("mine.show1")}}</p>
        <p>{{$t("mine.show2")}}</p>
      </div>
    </div>
    <div class="list-box">
      <ShowImg v-for="(v, i) in list" :link-index="i" :link="v" :img-show="true" :del-show="true" @del="deleteOne"
        :key="v" />
    </div>
  </div>
</template>

<style lang="scss" >
.my-list {
  @include center;

  .tip {
    width: 80%;
    border: 1px solid $themeFontColor;
    color: #0000008c;
    background-color: #f9f9f9;
    line-height: 1.6;
    padding: 10px;
    border-radius: 16px;
    margin-top: 28px;

    .title {
      @include vC;

      img {
        width: 17px;
        height: 17px;
        margin-left: 14px;
      }

      p {
        font-size: 20px;
        margin-left: 12px;
      }
    }

    .show {
      margin: 12px;

      p {
        margin-top: 8px;
      }
    }
  }

  .list-box {
    width: 100%;
    @include center;
    flex-direction: column;
  }
}
</style>
